package com.hejx.security.web.controller;

import com.hejx.security.common.bean.Result;
import com.hejx.security.common.enums.ResultEnum;
import com.hejx.security.common.utils.ColUtil;
import com.hejx.security.common.utils.ResultUtil;
import com.hejx.security.dto.OnlineUser;
import com.hejx.security.service.UserService;
import com.hejx.security.web.annotation.hasPermission;
import com.hejx.security.web.annotation.hasRole;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by hejx on 2017/5/8.
 */
@Controller
public class MainController extends BaseController {

    private static final Logger logger = LoggerFactory.getLogger(MainController.class);

    @Autowired
    private UserService userService;

    //主页
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        String welcomePage = "/admin/welcome";
        model.put("welcomePage",welcomePage);
        return "admin/main";
    }

    //跳转登录页面
    @RequestMapping(value = {"/","/login"},method = RequestMethod.GET)
    public String loginPage() {
        return LOGIN;
    }

    @RequestMapping("/error/{code}")
    public String errorPage(@PathVariable String code) {
        return "/error/"+code;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @ResponseBody
    public Result login(String username, String password) {
        OnlineUser onlineUser = userService.login(username, password);
        if(onlineUser == null){
            return ResultUtil.error(ResultEnum.PARAM_ERROR.getCode(),"用户名或密码错误");
        }else if(ColUtil.isEmpty(onlineUser.getRoles())){
            return ResultUtil.error(ResultEnum.INSUFFICIENT_AUTHORITY);
        }
        return ResultUtil.success();
        /**
        if("admin".equals(username) && "123456".equals(password)){
            return ResultUtil.success();
        }else{
            return ResultUtil.error(ResultEnum.PARAM_ERROR.getCode(),"用户名或密码错误");
        }
         **/
    }

    // get请求，访问添加用户 页面
    @RequestMapping(value = "/admin/welcome", method = RequestMethod.GET)
    public String welcome() {
        // 返回 admin/addUser.jsp页面
        return "admin/welcome";
    }

    //用户登出
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession httpSession) {
        //清空session内的值
        if(super.getNowUser()==null){
            return "redirect:/";
        }
        logger.info(super.getNowUser().getUsername()+"登出了系统...");
        httpSession.invalidate();
//        request.getSession().removeAttribute("xName"); //仅清空session中名称为xName的变量值，整个session其它不受影响；
        return "redirect:/";
    }

    @RequestMapping("/testRole")
    @hasRole(name = "admin222")
    public String testRole(){
        System.out.println("testRole...");
        return null;
    }

    @RequestMapping("/testPermission")
    @hasPermission(permission = "permission:test")
    public String testPermission(){
        System.out.println("testPermission...");
        return null;
    }




}
